package com.netty.esc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netty.esc.Interceptor.annotation.ControllerMapping;
import com.netty.esc.Interceptor.annotation.DontFuckMe;
import com.netty.esc.Interceptor.annotation.FuckAll;
import com.netty.esc.common.enums.MessageTypeEnum;
import com.netty.esc.entity.model.PersonTalkDTO;
import com.netty.esc.service.isolate.CommonFriendService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@FuckAll
@ControllerMapping("test")
@Slf4j
public class TestController {

    @Autowired
    private ThreadPoolExecutor scheduleThreadPoolExecutor;

    @Resource
    CommonFriendService commonFriendService;


    @Resource
    MongoTemplate mongoTemplate;

    @DontFuckMe
    @PostMapping("test1")
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    public String test1(@RequestBody JSONObject object){
        int a = 1+1;
        return "SUCCESS";
    }

    @PostMapping("test2")
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    public String test2(@RequestBody JSONObject object){
        int a = 1+1;
        scheduleThreadPoolExecutor.execute(()->{int ax = 1/0;});

        return "SUCCESS";
    }

    @GetMapping("test3")
    public void test3(HttpServletRequest request) throws InterruptedException {
        log.info("ip:{},线程:{}", request.getRemoteAddr(), Thread.currentThread().getName());
        Thread.sleep(500);
    }

    @GetMapping("test4")
    public void test4() throws InterruptedException {
        PersonTalkDTO personTalkDTO = new PersonTalkDTO();
        personTalkDTO.setGroupId(1L);
//        personTalkDTO.setMessageTypeEnum(MessageTypeEnum.MESSAGE);
        personTalkDTO.setMsg("hi");
        personTalkDTO.setReceiverId(14L);
        personTalkDTO.setUserId(15L);
        personTalkDTO.setCreateTime(new Date());
        mongoTemplate.save(personTalkDTO);
        Criteria criteria = Criteria.where("userId").is(12);
        Query query = new Query(criteria);
        List<PersonTalkDTO> personTalkDTOS = mongoTemplate.find(query, PersonTalkDTO.class);
        System.out.println(JSON.toJSONString(personTalkDTOS));

    }
}
